<?php
Database($_SESSION['cid']);
header("content-type:text/html;charset=utf-8");
class AddressAction extends Action{

//-----------------------------select--------------------------------------------------
//-----------------------------insert--------------------------------------------------
	public function addAct(){
		$rs =D('Address')->add_floor($_POST);
		if($rs)
			$this->success('添加成功',__URL__);
		else
			$this->error('添加失败');
	}
//-----------------------------update--------------------------------------------------
//-----------------------------delete--------------------------------------------------
	public function delAct()
	{
		switch($_GET['type'])
		{
			case 1:
				$rs = D('Address')->delete_location($_GET['id']);
				break;
			case 2:
				$rs = D('Address')->delete_building($_GET['id']);
				break;
			case 3:
				$rs = D('Address')->delete_floor($_GET['id']);
				break;
		}
		if($rs)
			echo json_encode(array('state'=>0));
		else
			echo json_encode(array('state'=>-1));
	}



	public function index(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);
		$local = D('Comp any/Address')->get_local();
		if($_POST['local_id']){
			$post['local_id'] = $_POST['local_id'];
		}else if($local[0]['local_id']){
			$post['local_id'] = $local[0]['local_id'];
		}		
		$building = D('Company/Address')->get_building($post['local_id']);
		
		if($_POST['building_id']){
			$post['building_id'] = $_POST['building_id'];
		}else if($building[0]['building_id']){
			$post['building_id'] = $building[0]['building_id'];
		}
		$floor = D('Company/Address')->get_floor($post['building_id']);
				
		$this->assign("building",$building);
		$this->assign("floor",$floor);
		$this->assign("local",$local);
		$this->display();		
	}
	public function building(){
		if($_POST){
			if($_POST['local_id']){
				$post['local_id'] = $_POST['local_id'];			
			}		
			$building = D('Company/Address')->get_building($post['local_id']);
			$post['building_id'] = $building[0]['building_id'];			
			$floor = D('Company/Address')->get_floor($post['building_id']);
			$this->assign("building",$building);
			$this->assign("floor",$floor);
			$this->display();
		}
		
	}
	
	public function floor(){
		if($_POST){			
			if($_POST['building_id']){				
				$post['building_id'] = $_POST['building_id'];
			}
			$floor = D('Company/Address')->get_floor($post['building_id']);
		}		
		$this->assign("floor",$floor);
		$this->display();
	}
	
//城市修改
	public function savec(){
		if($_POST){
			$post = $_POST;
			$save = D('Company/Address')->save_city($post);
			if($save){
				echo "<div class='btn btn-danger'>数据修改成功,<a href=".__URL__."/index>点击返回列表</a></div>";			
			}else{				
				echo "<div>数据修改失败,<a class='back'>点击重新修改</a><div><script>$('.back').click(function(){
					$('.box_city').css({display:'block'});
					$('.result').css({display:'none'});  
				})</script>";
			}
		}
	}
//楼座修改
	public function saveb(){
		if($_POST){
			$post['local_id'] = $_POST['local_id'];
			$post['building_name'] = $_POST['building_name'];			
			$save = D('Company/Address')->save_building($_POST['building_id'],$post);			
			if($save){
				echo "<div class='btn btn-danger'>数据修改成功,<a href=".__URL__."/index>点击返回列表</a></div>";			
			}else{				
				echo "<div>数据修改失败,<a class='back_build'>点击重新修改</a><div><script>$('.back_build').click(function(){
					$('.box_build').css({display:'block'});
					$('.result_build').css({display:'none'});  
				})</script>";
			}
			
		}
	}
	
//楼层修改
	public function savef(){
		if($_POST){			
			$post['floor_name'] = $_POST['floor_name'];
			$post['local_id'] = $_POST['local_id'];
			$post['building_id'] = $_POST['building_id'];			
			$save = D('Company/Address')->save_floor($_POST['floor_id'],$post);			
			if($save){
				echo "<div class='btn btn-danger'>数据修改成功,<a href=".__URL__."/index>点击返回列表</a></div>";			
			}else{				
				echo "<div>数据修改失败,<a class='back_floor'>点击重新修改</a><div><script>$('.back_floor').click(function(){
					$('.box_floor').css({display:'block'});
					$('.result_floor').css({display:'none'});  
				})</script>";
			}
		}
	}
	
//楼座查询	
	public function build_floor(){
		if($_POST){
			if($_POST['local_id']){
				$post['local_id'] = $_POST['local_id'];			
			};		
			$building = D('Company/Address')->get_building($post['local_id']);
			if($building){
				foreach($building as $value){
					echo "<option value='".$value['building_id']."'>".$value['building_name']."</option>";
				};				
			}
		}
		
	}
//地址添加
	public function forAct(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);
		$local = D('Company/Address')->get_local();
		if($_POST['local_id']){
			$post['local_id'] = $_POST['local_id'];
		}else if($local[0]['local_id']){
			$post['local_id'] = $local[0]['local_id'];
		}		
		$build = D('Company/Address')->get_building($post['local_id']);
		
		if($_POST['building_id']){
			$post['building_id'] = $_POST['building_id'];
		}else if($build[0]['building_id']){
			$post['building_id'] = $build[0]['building_id'];
		}
		$floor = D('Company/Address')->get_floor($post['building_id']);
		$this->assign('local',$local);
		$this->assign('build',$build);
		$this->assign('floor',$floor);
		$this->display();
	}
//添加城市
	public function add_city(){
		if($_POST){			
			$result = D('Company/Address')->name_local($_POST['local_name']);
			if($result){
				echo "<div class='back btn btn-danger' style='float:right;font-size:20px;'>数据已存在,点击回到编辑页面</div>";
			}else{
				$add = D('Company/Address')->add_city($_POST);
				if($add){
					echo  "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
				}else{
					echo "<div class='back btn btn-danger' style='float:right;font-size:20px;'>添加失败,点击回到编辑页面</div>";
				}
				
			};
		};
	}
//添加楼座
	public function add_build(){
		if($_POST['local_id']!='' && $_POST['building_name'] != '')
			$add = M('meeting_roombuilding','','DB_MEETING')->add($_POST);
		if($add){
			echo "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
		}else{
			echo "<div class='back btn btn-danger' style='float:right;font-size:20px;'>添加失败,点击回到编辑页面</div>";
		}
	}

	public function get_build()
	{
		$rs = M('meeting_roombuilding','','DB_MEETING')->where('local_id='.$_POST['local_id'])->select();
		if($rs)
			echo json_encode(array('state'=>0,'data'=>$rs));
		else
			echo json_encode(array('state'=>-1));
	}
}

?>